package com.ltedu.common.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @className: LongJsonDeserializer
 * @auther: Jerry
 * @date: 2020/12/7 17:29
 * @desctiption: TODO
 * @version: 1.0
 */
public class LongJsonDeserializer extends JsonDeserializer<Long> {

    private Logger logger = LoggerFactory.getLogger(LongJsonDeserializer.class);

    /**
     * 反序列化
     *
     * @param jsonParser             json解析器
     * @param deserializationContext 反序列化的上下文
     * @return {@link Long}* @throws IOException ioexception
     * @throws JsonProcessingException json处理异常
     */
    @Override
    public Long deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        String value = jsonParser.getText();
        try {
            return value == null ? null : Long.parseLong(value);
        } catch (NumberFormatException e) {
            logger.error("数据转换异常", e);
            return null;
        }
    }
}
